<?php

namespace Member\Controller;

/**
 * RoleController.class 控制器
 * @since 1.0 <2015-5-23> SoChishun <14507247@qq.com> Added.
 */
class Role extends AppbaseController {

    function indexSearch() {
        $search = I('param.');
        if (!empty($search['word'])) {
            $word = $search['word'];
            $where['_string'] = "title like '%$word%' or code like '%$word%'";
        }
        // $where['site_id'] = $this->site_id;
        return array('search' => $search, 'where' => $where);
    }

    /**
     * role_list操作
     * @since 1.0<2015-5-23> SoChishun Added.
     */
    public function index() {
        $asearch = $this->indexSearch();        
        $mRole = new \XWAM\Model\RoleModel();
        $list = $mRole->selectPaging($page, array('where' => $asearch['where'], 'order' => 'sort,id'));
        $this->assign('search', $asearch['search']);
        $this->assign('tree', $list);
        $this->assign('page', $page->showBootstrap());
        $this->display();
    }

    public function getTreeJson() {
        // $where['site_id'] = $this->site_id;
        $mRole = new \XWAM\Model\RoleModel();
        $list = $mRole->where($where)->field('id, title as text')->select();
        $this->ajaxReturn($list);
    }

    /**
     * 导出数据
     * @since 1.0 <2014-6-13> SoChishun Added.
     */
    public function export() {
        if (!$this->isSuperAdmin) {
            die('没有权限!');
        }
        // $where['site_id'] = $this->site_id;
        $mRole = new \XWAM\Model\RoleModel();
        $list = $mRole->where($where)->select();
        if (!$list) {
            $this->ajaxMsg(false, '找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    public function copy($id = 0) {
        if (!$id) {
            $this->ajaxMsg(false, '参数无效');
        }
        $mRole = new \XWAM\Model\RoleModel();
        $mRole->copyRole($id);
        $this->ajaxMsg(true);
    }

    /**
     * role_edit操作
     * @param int $id 主键编号
     * @since 1.0<2015-5-23> SoChishun Added.
     */
    public function edit($id = 0) {
        $data = array();
        if ($id) {
            $mRole = new \XWAM\Model\RoleModel();
            $data = $mRole->find($id);
        }
        if (!$data) {
            $data = array('site_id' => $this->siteId);
        }
        $this->assign('data', $data);
        $this->assign('meta_title', $meta_title);
        $this->display();
    }

    /**
     * role_edit_save操作
     * @since 1.0<2015-5-23> SoChishun Added.
     */
    public function editSave() {
        $mRole = new \XWAM\Model\RoleModel();
        $result = $mRole->saveRole();
        $this->dialogJump($result['status'], $result['info']);
    }

    /**
     * role_delete操作
     * @param string $id 主键编号
     * @since 1.0<2015-7-31> SoChishun Added.
     */
    public function delete($id = '') {
        $mRole = new \XWAM\Model\RoleModel();
        $this->ajaxReturn($mRole->deleteRole($id));
    }

    public function permission_edit($id = 0) {
        $mRole = new \XWAM\Model\RoleModel();
        $rules = $mRole->where(array('id' => $id))->getField('rules');
        $m_rule = new \XWAM\Model\RuleModel();
        $tree = $m_rule->select_tree();
        $this->assign('tree', $tree);
        $this->assign('role_id', $id);
        $this->assign('ids', $rules);
        $this->display();
    }

    public function permissionEditSave() {
        $ids = I('id');
        $role_id = I('role_id');
        if (!$role_id || !$ids) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $mRole = new \XWAM\Model\RoleModel();
        $result = $mRole->saveRule($role_id, $ids);
        $this->dialogJump($result['status'], $result['info']);
    }

    function changeStatus($id, $status) {
        $mRole = new \XWAM\Model\RoleModel();
        $this->ajaxReturn($mRole->changeStatus($id, $status));
    }

    function changeSort($id, $sort) {
        $mRole = new \XWAM\Model\RoleModel();
        $this->ajaxReturn($mRole->changeSort($id, $sort));
    }
    // 2017-3-18 同步用户表的角色标题
    function synchronous(){
        \XWAM\Model\RoleModel::updateForeignTitle(true);
    }

}
